<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>Spring源码 - 程序入口和XML解析 - 暗夜零星</title><meta name="Description" content="Spring源码的程序入口，和XML解析的源码流程。"><meta property="og:title" content="Spring源码 - 程序入口和XML解析" />
<meta property="og:description" content="Spring源码的程序入口，和XML解析的源码流程。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://wlizhi.cc/posts/spring/01-spring-main-xml-parse/" /><meta property="og:image" content="https://wlizhi.cc/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2020-11-22T12:00:00+08:00" />
<meta property="article:modified_time" content="2020-11-22T12:00:00+08:00" />

<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://wlizhi.cc/logo.png"/>

<meta name="twitter:title" content="Spring源码 - 程序入口和XML解析"/>
<meta name="twitter:description" content="Spring源码的程序入口，和XML解析的源码流程。"/>
<meta name="application-name" content="暗夜零星">
<meta name="apple-mobile-web-app-title" content="暗夜零星"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://wlizhi.cc/posts/spring/01-spring-main-xml-parse/" /><link rel="prev" href="https://wlizhi.cc/posts/life/memory-lol/" /><link rel="next" href="https://wlizhi.cc/posts/spring/02-spring-tag-parse-bean-definition-parser/" /><link rel="stylesheet" href="/lib/normalize/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "Spring源码 - 程序入口和XML解析",
        "inLanguage": "",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/wlizhi.cc\/posts\/spring\/01-spring-main-xml-parse\/"
        },"genre": "posts","keywords": "spring源码","wordcount":  5823 ,
        "url": "https:\/\/wlizhi.cc\/posts\/spring\/01-spring-main-xml-parse\/","datePublished": "2020-11-22T12:00:00+08:00","dateModified": "2020-11-22T12:00:00+08:00","publisher": {
            "@type": "Organization",
            "name": "Wlizhi"},"author": {
                "@type": "Person",
                "name": "Wlizhi"
            },"description": "Spring源码的程序入口，和XML解析的源码流程。"
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="暗夜零星"><span class="header-title-pre"><i class='far fa-moon fa-fw'></i></span>暗夜零星</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 所有文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="/message-board/"> 留言 </a><a class="menu-item" href="/about/"> 关于 </a><a class="menu-item" href="https://github.com/wlizhi" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i>  </a><a class="menu-item" href="https://gitee.com/wlizhi" title="Gitee" rel="noopener noreffer" target="_blank"><i class='fab fa-git fa-fw'></i>  </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="点击搜索..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="暗夜零星"><span class="header-title-pre"><i class='far fa-moon fa-fw'></i></span>暗夜零星</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="点击搜索..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">所有文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="/message-board/" title="">留言</a><a class="menu-item" href="/about/" title="">关于</a><a class="menu-item" href="https://github.com/wlizhi" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i></a><a class="menu-item" href="https://gitee.com/wlizhi" title="Gitee" rel="noopener noreffer" target="_blank"><i class='fab fa-git fa-fw'></i></a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">Spring源码 - 程序入口和XML解析</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="https://wlizhi.cc" title="Author" target="_blank" rel="noopener noreffer author" class="author"><i class="fas fa-user-circle fa-fw"></i>Wlizhi</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/spring/"><i class="far fa-folder fa-fw"></i>spring</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2020-11-22">2020-11-22</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 5823 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 12 分钟&nbsp;<span id="/posts/spring/01-spring-main-xml-parse/" class="leancloud_visitors" data-flag-title="Spring源码 - 程序入口和XML解析">
                        <i class="far fa-eye fa-fw"></i>&nbsp;<span class=leancloud-visitors-count></span>&nbsp;次阅读
                    </span>&nbsp;</div>
        </div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#1-spring的历史">1 Spring的历史</a></li>
    <li><a href="#2-读源码前的准备">2 读源码前的准备</a></li>
    <li><a href="#3-spring源码下载">3 Spring源码下载</a></li>
    <li><a href="#4-入口方法">4 入口方法</a></li>
    <li><a href="#5-xml解析流程">5 Xml解析流程</a></li>
    <li><a href="#6-创建xmlbeandefinitionreader对象">6 创建XmlBeanDefinitionReader对象</a></li>
    <li><a href="#7-创建beandefinitiondocumentreader对象">7 创建BeanDefinitionDocumentReader对象</a></li>
    <li><a href="#8-标签解析入口">8 标签解析入口</a></li>
    <li><a href="#9-解析默认标签">9 解析默认标签</a></li>
    <li><a href="#10-解析自定义标签">10 解析自定义标签</a></li>
    <li><a href="#11-总结">11 总结</a></li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><p>spring源码剖析，传送门： <a href="https://gitee.com/wlizhi/spring-framework" target="_blank" rel="noopener noreffer">gitee 仓库</a> 。</p>
<hr>
<h2 id="1-spring的历史">1 Spring的历史</h2>
<ol>
<li>2002年10月,Rod Johnson发布《Expert One-on-One J2EE设计和开发》一书</li>
<li>2004年3月，Spring1.0发布</li>
<li>2006年10 月，Spring2.0发布</li>
<li>2009年12月，Spring3.0发布</li>
<li>2013年12月，发布Spring4.0</li>
<li>2017年9月，Spring5.0发布</li>
</ol>
<h2 id="2-读源码前的准备">2 读源码前的准备</h2>
<ol>
<li>JDK1.8版本</li>
<li>spring 5.1.3.RELEASE版本</li>
<li>安装配置gradle</li>
</ol>
<h2 id="3-spring源码下载">3 Spring源码下载</h2>
<ol>
<li><code>git clone --branch v5.1.3.RELEASE https://gitee.com/wlizhi/spring-framework</code></li>
<li>gradle 下载，gradle 要 JDK8 的版本</li>
<li>到下载的spring源码路径执行 gradle 命令，gradlew :spring-oxm:compileTestJava</li>
<li>用idea打开spring源码工程。（<del>在idea中安装插件kotlin，重启idea，跟IDEA版本有关较新版本不用安装</del>）</li>
<li>把编译好的源码导入到工程中</li>
</ol>
<h2 id="4-入口方法">4 入口方法</h2>
<p>容器：AbstractApplicationContext 抽象父类，核心方法 refresh()。</p>
<ul>
<li>ClassPathXmlApplicationContext XML方式启动。</li>
<li>AnnotationConfigWebApplicationContext 注解方式启动，对局部代码进行测试时比较好用。</li>
<li>AnnotationConfigServletWebServerApplicationContext SpringBoot启动默认使用的上下文类。</li>
</ul>
<p>spring容器启动从创建ApplicationContext实例开始。以ClassPathXmlApplicationContext为例。
<figure><a class="lightgallery" href="https://oss.wlizhi.cc/blog/spring/ClassPathXmlApplicationContext.png" title="ClassPathXmlApplicationContext UML图" data-thumbnail="https://oss.wlizhi.cc/blog/spring/ClassPathXmlApplicationContext.png" data-sub-html="<h2>ClassPathXmlApplicationContext UML图</h2><p>ClassPathXmlApplicationContext UML图</p>">
        <img
            class="lazyload"
            src="/svg/loading.min.svg"
            data-src="https://oss.wlizhi.cc/blog/spring/ClassPathXmlApplicationContext.png"
            data-srcset="https://oss.wlizhi.cc/blog/spring/ClassPathXmlApplicationContext.png, https://oss.wlizhi.cc/blog/spring/ClassPathXmlApplicationContext.png 1.5x, https://oss.wlizhi.cc/blog/spring/ClassPathXmlApplicationContext.png 2x"
            data-sizes="auto"
            alt="https://oss.wlizhi.cc/blog/spring/ClassPathXmlApplicationContext.png" />
    </a><figcaption class="image-caption">ClassPathXmlApplicationContext UML图</figcaption>
    </figure></p>
<h2 id="5-xml解析流程">5 Xml解析流程</h2>
<p>首先进入spring容器启动的核心方法：refresh() ，创建BeanFactory对象 obtainFreshBeanFactory()</p>
<ol>
<li>创建XmlBeanDefinitionReader对象</li>
<li>通过Reader对象加载配置文件</li>
<li>根据加载的配置文件把配置文件封装成document</li>
<li>创建BeanDefinitionDocumentReader，DocumentReader负责对document解析</li>
<li>parseDefaultElement(ele, delegate);负责常规标签解析</li>
<li>delegate.parseCustomElement(ele);负责自定义标签解析</li>
<li>最终解析的标签封装成BeanDefinition并缓存到容器中</li>
</ol>
<h2 id="6-创建xmlbeandefinitionreader对象">6 创建XmlBeanDefinitionReader对象</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">AbstractXmlApplicationContext</span> <span class="kd">extends</span> <span class="n">AbstractRefreshableConfigApplicationContext</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">	<span class="nd">@Override</span>
</span></span><span class="line"><span class="cl">	<span class="kd">protected</span> <span class="kt">void</span> <span class="nf">loadBeanDefinitions</span><span class="o">(</span><span class="n">DefaultListableBeanFactory</span> <span class="n">beanFactory</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">BeansException</span><span class="o">,</span> <span class="n">IOException</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// Create a new XmlBeanDefinitionReader for the given BeanFactory.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// 创建BeanDefinitionReader
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">XmlBeanDefinitionReader</span> <span class="n">beanDefinitionReader</span> <span class="o">=</span> <span class="k">new</span> <span class="n">XmlBeanDefinitionReader</span><span class="o">(</span><span class="n">beanFactory</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">		<span class="c1">// Configure the bean definition reader with this context&#39;s
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// resource loading environment.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">beanDefinitionReader</span><span class="o">.</span><span class="na">setEnvironment</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">getEnvironment</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">		<span class="n">beanDefinitionReader</span><span class="o">.</span><span class="na">setResourceLoader</span><span class="o">(</span><span class="k">this</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="n">beanDefinitionReader</span><span class="o">.</span><span class="na">setEntityResolver</span><span class="o">(</span><span class="k">new</span> <span class="n">ResourceEntityResolver</span><span class="o">(</span><span class="k">this</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">		<span class="c1">// Allow a subclass to provide custom initialization of the reader,
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// then proceed with actually loading the bean definitions.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">initBeanDefinitionReader</span><span class="o">(</span><span class="n">beanDefinitionReader</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// TODO 关键点：加载BeanDefinition
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">loadBeanDefinitions</span><span class="o">(</span><span class="n">beanDefinitionReader</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>在loadBeanDefinitions方法的第一行，创建了XmlBeanDefinitionReader对象。这个类的主要作用是读取BeanDefinition，并将XML文档的读取委托给BeanDefinitionDocumentReader。</p>
<h2 id="7-创建beandefinitiondocumentreader对象">7 创建BeanDefinitionDocumentReader对象</h2>
<p>来到loadBeanDefinitions(beanDefinitionReader);这里将要读取的xml路径传递进来，循环xml路径，依次进行解析。<br>
<font color="orange">注：看注释中的关键点</font></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">AbstractBeanDefinitionReader</span> <span class="kd">implements</span> <span class="n">BeanDefinitionReader</span><span class="o">,</span> <span class="n">EnvironmentCapable</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 首先进入这个方法
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="nd">@Override</span>
</span></span><span class="line"><span class="cl">	<span class="kd">public</span> <span class="kt">int</span> <span class="nf">loadBeanDefinitions</span><span class="o">(</span><span class="n">String</span><span class="o">...</span> <span class="n">locations</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">BeanDefinitionStoreException</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="n">Assert</span><span class="o">.</span><span class="na">notNull</span><span class="o">(</span><span class="n">locations</span><span class="o">,</span> <span class="s">&#34;Location array must not be null&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="n">0</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 一次解析配置文件，加载配置文件中定义的Bean。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">for</span> <span class="o">(</span><span class="n">String</span> <span class="n">location</span> <span class="o">:</span> <span class="n">locations</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">count</span> <span class="o">+=</span> <span class="n">loadBeanDefinitions</span><span class="o">(</span><span class="n">location</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">count</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>路径最终会被封装为InputSource对象，使用委托模式，交给BeanDefinitionDocumentReader处理</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">XmlBeanDefinitionReader</span> <span class="kd">extends</span> <span class="n">AbstractBeanDefinitionReader</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 第一步：入口
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kd">protected</span> <span class="kt">int</span> <span class="nf">doLoadBeanDefinitions</span><span class="o">(</span><span class="n">InputSource</span> <span class="n">inputSource</span><span class="o">,</span> <span class="n">Resource</span> <span class="n">resource</span><span class="o">)</span>
</span></span><span class="line"><span class="cl">			<span class="kd">throws</span> <span class="n">BeanDefinitionStoreException</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="k">try</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// 解析xml配置文件，获取到Document对象
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">Document</span> <span class="n">doc</span> <span class="o">=</span> <span class="n">doLoadDocument</span><span class="o">(</span><span class="n">inputSource</span><span class="o">,</span> <span class="n">resource</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// 解析、注册BeanDefinition
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="n">registerBeanDefinitions</span><span class="o">(</span><span class="n">doc</span><span class="o">,</span> <span class="n">resource</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="k">if</span> <span class="o">(</span><span class="n">logger</span><span class="o">.</span><span class="na">isDebugEnabled</span><span class="o">())</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="n">logger</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">&#34;Loaded &#34;</span> <span class="o">+</span> <span class="n">count</span> <span class="o">+</span> <span class="s">&#34; bean definitions from &#34;</span> <span class="o">+</span> <span class="n">resource</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">			<span class="k">return</span> <span class="n">count</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">        <span class="c1">//此处分别捕获了很多种异常，省略了。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">ex</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">          			<span class="k">throw</span> <span class="n">ex</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 第二步 创建BeanDefinitionDocumentReader，委托给BeanDefinitionDocumentReader进行解析工作。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="kd">public</span> <span class="kt">int</span> <span class="nf">registerBeanDefinitions</span><span class="o">(</span><span class="n">Document</span> <span class="n">doc</span><span class="o">,</span> <span class="n">Resource</span> <span class="n">resource</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">BeanDefinitionStoreException</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 委托模式，将Document的解析及BeanDefinition的注册交给BeanDefinitionDocumentReader
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// 这里使用的DefaultBeanDefinitionDocumentReader进行解析
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">BeanDefinitionDocumentReader</span> <span class="n">documentReader</span> <span class="o">=</span> <span class="n">createBeanDefinitionDocumentReader</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">		<span class="kt">int</span> <span class="n">countBefore</span> <span class="o">=</span> <span class="n">getRegistry</span><span class="o">().</span><span class="na">getBeanDefinitionCount</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// TODO 关键点：解析xml、注册BeanDefinition
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">documentReader</span><span class="o">.</span><span class="na">registerBeanDefinitions</span><span class="o">(</span><span class="n">doc</span><span class="o">,</span> <span class="n">createReaderContext</span><span class="o">(</span><span class="n">resource</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">getRegistry</span><span class="o">().</span><span class="na">getBeanDefinitionCount</span><span class="o">()</span> <span class="o">-</span> <span class="n">countBefore</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="8-标签解析入口">8 标签解析入口</h2>
<p>在解析标签之前、和之后，分别有一个钩子方法。这两个钩子方法分别是 解析xml并注册BeanDefinition 之前和之后的扩展方法，ClassPathXmlApplicationContext中，这两个方法体都是空的。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">DefaultBeanDefinitionDocumentReader</span> <span class="kd">implements</span> <span class="n">BeanDefinitionDocumentReader</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 第一步
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">doRegisterBeanDefinitions</span><span class="o">(</span><span class="n">Element</span> <span class="n">root</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// Any nested &lt;beans&gt; elements will cause recursion in this method. In
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// order to propagate and preserve &lt;beans&gt; default-* attributes correctly,
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// keep track of the current (parent) delegate, which may be null. Create
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// the new (child) delegate with a reference to the parent for fallback purposes,
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// then ultimately reset this.delegate back to its original (parent) reference.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// this behavior emulates a stack of delegates without actually necessitating one.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">BeanDefinitionParserDelegate</span> <span class="n">parent</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">delegate</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// BeanDefinitionParserDelegate中封装了预定义的很多标签属性
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">this</span><span class="o">.</span><span class="na">delegate</span> <span class="o">=</span> <span class="n">createDelegate</span><span class="o">(</span><span class="n">getReaderContext</span><span class="o">(),</span> <span class="n">root</span><span class="o">,</span> <span class="n">parent</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">		<span class="c1">// 根标签解析 NameSpace：http://www.springframework.org/schema/beans
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">delegate</span><span class="o">.</span><span class="na">isDefaultNamespace</span><span class="o">(</span><span class="n">root</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">String</span> <span class="n">profileSpec</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="na">getAttribute</span><span class="o">(</span><span class="n">PROFILE_ATTRIBUTE</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="k">if</span> <span class="o">(</span><span class="n">StringUtils</span><span class="o">.</span><span class="na">hasText</span><span class="o">(</span><span class="n">profileSpec</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="n">String</span><span class="o">[]</span> <span class="n">specifiedProfiles</span> <span class="o">=</span> <span class="n">StringUtils</span><span class="o">.</span><span class="na">tokenizeToStringArray</span><span class="o">(</span>
</span></span><span class="line"><span class="cl">						<span class="n">profileSpec</span><span class="o">,</span> <span class="n">BeanDefinitionParserDelegate</span><span class="o">.</span><span class="na">MULTI_VALUE_ATTRIBUTE_DELIMITERS</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="c1">// We cannot use Profiles.of(...) since profile expressions are not supported
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>				<span class="c1">// in XML config. See SPR-12458 for details.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>				<span class="k">if</span> <span class="o">(!</span><span class="n">getReaderContext</span><span class="o">().</span><span class="na">getEnvironment</span><span class="o">().</span><span class="na">acceptsProfiles</span><span class="o">(</span><span class="n">specifiedProfiles</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">					<span class="k">if</span> <span class="o">(</span><span class="n">logger</span><span class="o">.</span><span class="na">isDebugEnabled</span><span class="o">())</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">						<span class="n">logger</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">&#34;Skipped XML bean definition file due to specified profiles [&#34;</span> <span class="o">+</span> <span class="n">profileSpec</span> <span class="o">+</span>
</span></span><span class="line"><span class="cl">								<span class="s">&#34;] not matching: &#34;</span> <span class="o">+</span> <span class="n">getReaderContext</span><span class="o">().</span><span class="na">getResource</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">					<span class="o">}</span>
</span></span><span class="line"><span class="cl">					<span class="k">return</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">				<span class="o">}</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 钩子方法，解析xml注册BeanDefinition之前调用，可以不看
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">preProcessXml</span><span class="o">(</span><span class="n">root</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// TODO 解析xml，注册BeanDefinition
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">parseBeanDefinitions</span><span class="o">(</span><span class="n">root</span><span class="o">,</span> <span class="k">this</span><span class="o">.</span><span class="na">delegate</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 钩子方法，解析xml注册BeanDefinition之后调用，可以不看
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">postProcessXml</span><span class="o">(</span><span class="n">root</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">		<span class="k">this</span><span class="o">.</span><span class="na">delegate</span> <span class="o">=</span> <span class="n">parent</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 第二步
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="kd">protected</span> <span class="kt">void</span> <span class="nf">parseBeanDefinitions</span><span class="o">(</span><span class="n">Element</span> <span class="n">root</span><span class="o">,</span> <span class="n">BeanDefinitionParserDelegate</span> <span class="n">delegate</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// Spring根标签，NameSpace：http://www.springframework.org/schema/beans
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// 这里循环document中的元素，进行解析默认标签及自定义标签
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">String</span> <span class="n">tagName</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="na">getTagName</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">delegate</span><span class="o">.</span><span class="na">isDefaultNamespace</span><span class="o">(</span><span class="n">root</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">NodeList</span> <span class="n">nl</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="na">getChildNodes</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">			<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">nl</span><span class="o">.</span><span class="na">getLength</span><span class="o">();</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="n">Node</span> <span class="n">node</span> <span class="o">=</span> <span class="n">nl</span><span class="o">.</span><span class="na">item</span><span class="o">(</span><span class="n">i</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="k">if</span> <span class="o">(</span><span class="n">node</span> <span class="k">instanceof</span> <span class="n">Element</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">					<span class="n">Element</span> <span class="n">ele</span> <span class="o">=</span> <span class="o">(</span><span class="n">Element</span><span class="o">)</span> <span class="n">node</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">					<span class="k">if</span> <span class="o">(</span><span class="n">delegate</span><span class="o">.</span><span class="na">isDefaultNamespace</span><span class="o">(</span><span class="n">ele</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">						<span class="n">logger</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">&#34;delegate.isDefaultNamespace(root):true,解析默认标签 &#34;</span> <span class="o">+</span> <span class="n">tagName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">						<span class="c1">// TODO 关键点：解析默认标签
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>						<span class="n">parseDefaultElement</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">delegate</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">					<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">						<span class="n">logger</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">&#34;delegate.isDefaultNamespace(root):true,解析自定义标签 &#34;</span> <span class="o">+</span> <span class="n">tagName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">						<span class="c1">// TODO 关键点：解析自定义标签
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>						<span class="n">delegate</span><span class="o">.</span><span class="na">parseCustomElement</span><span class="o">(</span><span class="n">ele</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">					<span class="o">}</span>
</span></span><span class="line"><span class="cl">				<span class="o">}</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">logger</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">&#34;delegate.isDefaultNamespace(root):false,解析自定义标签 &#34;</span> <span class="o">+</span> <span class="n">tagName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// TODO 关键点：解析自定义标签
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">delegate</span><span class="o">.</span><span class="na">parseCustomElement</span><span class="o">(</span><span class="n">root</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="9-解析默认标签">9 解析默认标签</h2>
<ol>
<li>根据xml节点的名称，对比代码中定义的标签名称。跳转到对应的解析方法</li>
<li>以bean标签为例，最终走到BeanDefinitionParserDelegate.parseBeanDefinitionElement()方法</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">DefaultBeanDefinitionDocumentReader</span> <span class="kd">implements</span> <span class="n">BeanDefinitionDocumentReader</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 第一步 进入标签解析方法
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="kd">private</span> <span class="kt">void</span> <span class="nf">parseDefaultElement</span><span class="o">(</span><span class="n">Element</span> <span class="n">ele</span><span class="o">,</span> <span class="n">BeanDefinitionParserDelegate</span> <span class="n">delegate</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// import标签解析
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">if</span> <span class="o">(</span><span class="n">delegate</span><span class="o">.</span><span class="na">nodeNameEquals</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">IMPORT_ELEMENT</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">importBeanDefinitionResource</span><span class="o">(</span><span class="n">ele</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// alias标签解析
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">delegate</span><span class="o">.</span><span class="na">nodeNameEquals</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">ALIAS_ELEMENT</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">processAliasRegistration</span><span class="o">(</span><span class="n">ele</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// TODO 重点看：bean标签解析
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">delegate</span><span class="o">.</span><span class="na">nodeNameEquals</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">BEAN_ELEMENT</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">processBeanDefinition</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">delegate</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// beans标签解析
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">delegate</span><span class="o">.</span><span class="na">nodeNameEquals</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">NESTED_BEANS_ELEMENT</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// recurse
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">doRegisterBeanDefinitions</span><span class="o">(</span><span class="n">ele</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 第二步 进入bean标签解析方法
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">processBeanDefinition</span><span class="o">(</span><span class="n">Element</span> <span class="n">ele</span><span class="o">,</span> <span class="n">BeanDefinitionParserDelegate</span> <span class="n">delegate</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// TODO 重点：获取到BeanDefinitionHolder，内部封装了BeanDefinition
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">BeanDefinitionHolder</span> <span class="n">bdHolder</span> <span class="o">=</span> <span class="n">delegate</span><span class="o">.</span><span class="na">parseBeanDefinitionElement</span><span class="o">(</span><span class="n">ele</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">bdHolder</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// 如果必要，装饰BeanDefinition，这里用到了装饰模式
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">bdHolder</span> <span class="o">=</span> <span class="n">delegate</span><span class="o">.</span><span class="na">decorateBeanDefinitionIfRequired</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">bdHolder</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="k">try</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="c1">// Register the final decorated instance.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>				<span class="n">BeanDefinitionReaderUtils</span><span class="o">.</span><span class="na">registerBeanDefinition</span><span class="o">(</span><span class="n">bdHolder</span><span class="o">,</span> <span class="n">getReaderContext</span><span class="o">().</span><span class="na">getRegistry</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">BeanDefinitionStoreException</span> <span class="n">ex</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="n">getReaderContext</span><span class="o">().</span><span class="na">error</span><span class="o">(</span><span class="s">&#34;Failed to register bean definition with name &#39;&#34;</span> <span class="o">+</span>
</span></span><span class="line"><span class="cl">						<span class="n">bdHolder</span><span class="o">.</span><span class="na">getBeanName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&#34;&#39;&#34;</span><span class="o">,</span> <span class="n">ele</span><span class="o">,</span> <span class="n">ex</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// Send registration event.
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">getReaderContext</span><span class="o">().</span><span class="na">fireComponentRegistered</span><span class="o">(</span><span class="k">new</span> <span class="n">BeanComponentDefinition</span><span class="o">(</span><span class="n">bdHolder</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>解析和封装BeanDefinition，这里解析了bean标签中的所有属性，将这些属性值封装成BeanDefinition对象。最终会被注册到BeanDefinitionRegistry中。<br>
BeanDefinitionRegistry作为spring容器中BeanFactory的全局成员变量。也就是说，最终得到的BeanDefinition贯穿整个spring容器的生命周期，它是对spring容器中所有将要实例化的bean信息的描述。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">BeanDefinitionParserDelegate</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="nd">@Nullable</span>
</span></span><span class="line"><span class="cl">	<span class="kd">public</span> <span class="n">AbstractBeanDefinition</span> <span class="nf">parseBeanDefinitionElement</span><span class="o">(</span>
</span></span><span class="line"><span class="cl">			<span class="n">Element</span> <span class="n">ele</span><span class="o">,</span> <span class="n">String</span> <span class="n">beanName</span><span class="o">,</span> <span class="nd">@Nullable</span> <span class="n">BeanDefinition</span> <span class="n">containingBean</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">		<span class="k">this</span><span class="o">.</span><span class="na">parseState</span><span class="o">.</span><span class="na">push</span><span class="o">(</span><span class="k">new</span> <span class="n">BeanEntry</span><span class="o">(</span><span class="n">beanName</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">		<span class="c1">// bean标签中的class属性
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">String</span> <span class="n">className</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">ele</span><span class="o">.</span><span class="na">hasAttribute</span><span class="o">(</span><span class="n">CLASS_ATTRIBUTE</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">className</span> <span class="o">=</span> <span class="n">ele</span><span class="o">.</span><span class="na">getAttribute</span><span class="o">(</span><span class="n">CLASS_ATTRIBUTE</span><span class="o">).</span><span class="na">trim</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// bean标签中的parent属性
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">String</span> <span class="n">parent</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">ele</span><span class="o">.</span><span class="na">hasAttribute</span><span class="o">(</span><span class="n">PARENT_ATTRIBUTE</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">parent</span> <span class="o">=</span> <span class="n">ele</span><span class="o">.</span><span class="na">getAttribute</span><span class="o">(</span><span class="n">PARENT_ATTRIBUTE</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">		<span class="k">try</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// TODO 重点：创建BeanDefinition，设置parent、beanClass、beanClassName属性。
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="c1">// bean标签的bean均定义为：GenericBeanDefinition
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">AbstractBeanDefinition</span> <span class="n">bd</span> <span class="o">=</span> <span class="n">createBeanDefinition</span><span class="o">(</span><span class="n">className</span><span class="o">,</span> <span class="n">parent</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">			<span class="c1">// TODO 重点：解析element并设置BeanDefinition中的属性值
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">parseBeanDefinitionAttributes</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">beanName</span><span class="o">,</span> <span class="n">containingBean</span><span class="o">,</span> <span class="n">bd</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="n">bd</span><span class="o">.</span><span class="na">setDescription</span><span class="o">(</span><span class="n">DomUtils</span><span class="o">.</span><span class="na">getChildElementValueByTagName</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">DESCRIPTION_ELEMENT</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">			<span class="c1">// 解析meta标签、lookupOverride子标签、replaceMethod子标签
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">parseMetaElements</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">bd</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="n">parseLookupOverrideSubElements</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">bd</span><span class="o">.</span><span class="na">getMethodOverrides</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">			<span class="n">parseReplacedMethodSubElements</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">bd</span><span class="o">.</span><span class="na">getMethodOverrides</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// 解析构造函数参数、property参数、qualifier参数
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="n">parseConstructorArgElements</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">bd</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="n">parsePropertyElements</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">bd</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="n">parseQualifierElements</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="n">bd</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">			<span class="n">bd</span><span class="o">.</span><span class="na">setResource</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">readerContext</span><span class="o">.</span><span class="na">getResource</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">			<span class="n">bd</span><span class="o">.</span><span class="na">setSource</span><span class="o">(</span><span class="n">extractSource</span><span class="o">(</span><span class="n">ele</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">			<span class="k">return</span> <span class="n">bd</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">catch</span> <span class="o">(</span><span class="n">ClassNotFoundException</span> <span class="n">ex</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">error</span><span class="o">(</span><span class="s">&#34;Bean class [&#34;</span> <span class="o">+</span> <span class="n">className</span> <span class="o">+</span> <span class="s">&#34;] not found&#34;</span><span class="o">,</span> <span class="n">ele</span><span class="o">,</span> <span class="n">ex</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">catch</span> <span class="o">(</span><span class="n">NoClassDefFoundError</span> <span class="n">err</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">error</span><span class="o">(</span><span class="s">&#34;Class that bean class [&#34;</span> <span class="o">+</span> <span class="n">className</span> <span class="o">+</span> <span class="s">&#34;] depends on not found&#34;</span><span class="o">,</span> <span class="n">ele</span><span class="o">,</span> <span class="n">err</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">catch</span> <span class="o">(</span><span class="n">Throwable</span> <span class="n">ex</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">error</span><span class="o">(</span><span class="s">&#34;Unexpected failure during bean definition parsing&#34;</span><span class="o">,</span> <span class="n">ele</span><span class="o">,</span> <span class="n">ex</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">finally</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">this</span><span class="o">.</span><span class="na">parseState</span><span class="o">.</span><span class="na">pop</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="10-解析自定义标签">10 解析自定义标签</h2>
<p>SPI方式获取加载所有的namespaceUri（SPI这里不进行讨论），通过当前标签定义的namespaceUri，获取到初始化后（init()）的处理类。然后调用处理类的parse方法，在此方法中，完成了标签属性的解析和BeanDefinition注册</p>
<p>在resolve方法中，调用了NamespaceHandler的init方法，init方法中注册了标签属性的处理类。</p>
<p><strong>这些处理类都是BeanDefinitionParser的实现，在BeanDefinitionParser中，仅有一个parse方法，用来解析特定的标签属性。</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">BeanDefinitionParserDelegate</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">	<span class="nd">@Nullable</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="n">BeanDefinition</span> <span class="nf">parseCustomElement</span><span class="o">(</span><span class="n">Element</span> <span class="n">ele</span><span class="o">,</span> <span class="nd">@Nullable</span> <span class="n">BeanDefinition</span> <span class="n">containingBd</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// SPI方式获取namespaceUri
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="n">String</span> <span class="n">namespaceUri</span> <span class="o">=</span> <span class="n">getNamespaceURI</span><span class="o">(</span><span class="n">ele</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="o">(</span><span class="n">namespaceUri</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// 获取NamespaceHandler，这里面调用了NamespaceHandler的init方法
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="n">NamespaceHandler</span> <span class="n">handler</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">readerContext</span><span class="o">.</span><span class="na">getNamespaceHandlerResolver</span><span class="o">().</span><span class="na">resolve</span><span class="o">(</span><span class="n">namespaceUri</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="o">(</span><span class="n">handler</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">error</span><span class="o">(</span><span class="s">&#34;Unable to locate Spring NamespaceHandler for XML schema namespace [&#34;</span> <span class="o">+</span> <span class="n">namespaceUri</span> <span class="o">+</span> <span class="s">&#34;]&#34;</span><span class="o">,</span> <span class="n">ele</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="n">handler</span><span class="o">.</span><span class="na">parse</span><span class="o">(</span><span class="n">ele</span><span class="o">,</span> <span class="k">new</span> <span class="n">ParserContext</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">readerContext</span><span class="o">,</span> <span class="k">this</span><span class="o">,</span> <span class="n">containingBd</span><span class="o">));</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>NamespaceHandler，是根据对应模块中的META-INF/spring.handlers中定义的映射关系进行获取的（SPI方式获取）。<code>映射关系NamespaceUri=NamespaceHandler</code></p>
<p>以下是spring-context中定义的处理映射（<code>spring-context/META-INF/spring.handlers</code>）</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
</span></span><span class="line"><span class="cl">http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler
</span></span><span class="line"><span class="cl">http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler
</span></span><span class="line"><span class="cl">http\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler
</span></span><span class="line"><span class="cl">http\://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler
</span></span></code></pre></td></tr></table>
</div>
</div><p><strong>resolve方法获取了处理类对象，每个处理类实例化之后都会放入缓存handlerMappings，下次获取直接从缓存中获取。handlerMappings的键时NamespaceUri，值初始是对应处理类的ClassName，实例化之后会替换为该类的实例对象。</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">DefaultNamespaceHandlerResolver</span> <span class="kd">implements</span> <span class="n">NamespaceHandlerResolver</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="nd">@Override</span>
</span></span><span class="line"><span class="cl">	<span class="nd">@Nullable</span>
</span></span><span class="line"><span class="cl">	<span class="kd">public</span> <span class="n">NamespaceHandler</span> <span class="nf">resolve</span><span class="o">(</span><span class="n">String</span> <span class="n">namespaceUri</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 从缓存中获取，如果存在，直接返回，如果不存在则创建
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">Object</span><span class="o">&gt;</span> <span class="n">handlerMappings</span> <span class="o">=</span> <span class="n">getHandlerMappings</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">		<span class="n">Object</span> <span class="n">handlerOrClassName</span> <span class="o">=</span> <span class="n">handlerMappings</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">namespaceUri</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">handlerOrClassName</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">handlerOrClassName</span> <span class="k">instanceof</span> <span class="n">NamespaceHandler</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="k">return</span> <span class="o">(</span><span class="n">NamespaceHandler</span><span class="o">)</span> <span class="n">handlerOrClassName</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">else</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">String</span> <span class="n">className</span> <span class="o">=</span> <span class="o">(</span><span class="n">String</span><span class="o">)</span> <span class="n">handlerOrClassName</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="k">try</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="c1">// 加载Class
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>				<span class="n">Class</span><span class="o">&lt;?&gt;</span> <span class="n">handlerClass</span> <span class="o">=</span> <span class="n">ClassUtils</span><span class="o">.</span><span class="na">forName</span><span class="o">(</span><span class="n">className</span><span class="o">,</span> <span class="k">this</span><span class="o">.</span><span class="na">classLoader</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="k">if</span> <span class="o">(!</span><span class="n">NamespaceHandler</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">isAssignableFrom</span><span class="o">(</span><span class="n">handlerClass</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">					<span class="k">throw</span> <span class="k">new</span> <span class="n">FatalBeanException</span><span class="o">(</span><span class="s">&#34;Class [&#34;</span> <span class="o">+</span> <span class="n">className</span> <span class="o">+</span> <span class="s">&#34;] for namespace [&#34;</span> <span class="o">+</span> <span class="n">namespaceUri</span> <span class="o">+</span>
</span></span><span class="line"><span class="cl">							<span class="s">&#34;] does not implement the [&#34;</span> <span class="o">+</span> <span class="n">NamespaceHandler</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&#34;] interface&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="o">}</span>
</span></span><span class="line"><span class="cl">				<span class="c1">// TODO 重点：实例化NamespaceHandler，调用init方法，放入缓存。这里是一个扩展点，所有的自定义标签都要提供对应的NamespaceHandler
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>				<span class="c1">//  spring.handler文件中定义了很多内置的NamespaceHandler，通过SPI的方式加载、实例化
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>				<span class="n">NamespaceHandler</span> <span class="n">namespaceHandler</span> <span class="o">=</span> <span class="o">(</span><span class="n">NamespaceHandler</span><span class="o">)</span> <span class="n">BeanUtils</span><span class="o">.</span><span class="na">instantiateClass</span><span class="o">(</span><span class="n">handlerClass</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="n">namespaceHandler</span><span class="o">.</span><span class="na">init</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">				<span class="n">handlerMappings</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">namespaceUri</span><span class="o">,</span> <span class="n">namespaceHandler</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">				<span class="k">return</span> <span class="n">namespaceHandler</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">			<span class="k">catch</span> <span class="o">(</span><span class="n">ClassNotFoundException</span> <span class="n">ex</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="k">throw</span> <span class="k">new</span> <span class="n">FatalBeanException</span><span class="o">(</span><span class="s">&#34;Could not find NamespaceHandler class [&#34;</span> <span class="o">+</span> <span class="n">className</span> <span class="o">+</span>
</span></span><span class="line"><span class="cl">						<span class="s">&#34;] for namespace [&#34;</span> <span class="o">+</span> <span class="n">namespaceUri</span> <span class="o">+</span> <span class="s">&#34;]&#34;</span><span class="o">,</span> <span class="n">ex</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">			<span class="k">catch</span> <span class="o">(</span><span class="n">LinkageError</span> <span class="n">err</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">				<span class="k">throw</span> <span class="k">new</span> <span class="n">FatalBeanException</span><span class="o">(</span><span class="s">&#34;Unresolvable class definition for NamespaceHandler class [&#34;</span> <span class="o">+</span>
</span></span><span class="line"><span class="cl">						<span class="n">className</span> <span class="o">+</span> <span class="s">&#34;] for namespace [&#34;</span> <span class="o">+</span> <span class="n">namespaceUri</span> <span class="o">+</span> <span class="s">&#34;]&#34;</span><span class="o">,</span> <span class="n">err</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">			<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>每个自定义标签都有自己的NamespaceHandler实现。<br>
<figure><a class="lightgallery" href="https://oss.wlizhi.cc/blog/spring/NameSpaceHandlerImpls.png" title="https://oss.wlizhi.cc/blog/spring/NameSpaceHandlerImpls.png" data-thumbnail="https://oss.wlizhi.cc/blog/spring/NameSpaceHandlerImpls.png" data-sub-html="<h2>NamespaceHandler实现类</h2>">
        <img
            class="lazyload"
            src="/svg/loading.min.svg"
            data-src="https://oss.wlizhi.cc/blog/spring/NameSpaceHandlerImpls.png"
            data-srcset="https://oss.wlizhi.cc/blog/spring/NameSpaceHandlerImpls.png, https://oss.wlizhi.cc/blog/spring/NameSpaceHandlerImpls.png 1.5x, https://oss.wlizhi.cc/blog/spring/NameSpaceHandlerImpls.png 2x"
            data-sizes="auto"
            alt="https://oss.wlizhi.cc/blog/spring/NameSpaceHandlerImpls.png" />
    </a><figcaption class="image-caption">NamespaceHandler实现类</figcaption>
    </figure></p>
<p>以<code>context</code>标签为例，注册不同标签属性对应的处理类。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ContextNamespaceHandler</span> <span class="kd">extends</span> <span class="n">NamespaceHandlerSupport</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">	<span class="nd">@Override</span>
</span></span><span class="line"><span class="cl">	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">init</span><span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 注册各种标签属性的解析类
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">registerBeanDefinitionParser</span><span class="o">(</span><span class="s">&#34;property-placeholder&#34;</span><span class="o">,</span> <span class="k">new</span> <span class="n">PropertyPlaceholderBeanDefinitionParser</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">		<span class="n">registerBeanDefinitionParser</span><span class="o">(</span><span class="s">&#34;property-override&#34;</span><span class="o">,</span> <span class="k">new</span> <span class="n">PropertyOverrideBeanDefinitionParser</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">		<span class="n">registerBeanDefinitionParser</span><span class="o">(</span><span class="s">&#34;annotation-config&#34;</span><span class="o">,</span> <span class="k">new</span> <span class="n">AnnotationConfigBeanDefinitionParser</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">		<span class="n">registerBeanDefinitionParser</span><span class="o">(</span><span class="s">&#34;component-scan&#34;</span><span class="o">,</span> <span class="k">new</span> <span class="n">ComponentScanBeanDefinitionParser</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">		<span class="n">registerBeanDefinitionParser</span><span class="o">(</span><span class="s">&#34;load-time-weaver&#34;</span><span class="o">,</span> <span class="k">new</span> <span class="n">LoadTimeWeaverBeanDefinitionParser</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">		<span class="n">registerBeanDefinitionParser</span><span class="o">(</span><span class="s">&#34;spring-configured&#34;</span><span class="o">,</span> <span class="k">new</span> <span class="n">SpringConfiguredBeanDefinitionParser</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">		<span class="n">registerBeanDefinitionParser</span><span class="o">(</span><span class="s">&#34;mbean-export&#34;</span><span class="o">,</span> <span class="k">new</span> <span class="n">MBeanExportBeanDefinitionParser</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">		<span class="n">registerBeanDefinitionParser</span><span class="o">(</span><span class="s">&#34;mbean-server&#34;</span><span class="o">,</span> <span class="k">new</span> <span class="n">MBeanServerBeanDefinitionParser</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>上面代码registerBeanDefinitionParser方法，会将（标签属性名称-解析类的实例）缓存到NamespaceHandlerSupport.parsers变量中（这是一个map集合）。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">NamespaceHandlerSupport</span> <span class="kd">implements</span> <span class="n">NamespaceHandler</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 将（标签属性名称-解析类的实例）映射关系缓存起来，记着这个成员变量，后面就是从这个变量中获取解析类的
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kd">protected</span> <span class="kd">final</span> <span class="kt">void</span> <span class="nf">registerBeanDefinitionParser</span><span class="o">(</span><span class="n">String</span> <span class="n">elementName</span><span class="o">,</span> <span class="n">BeanDefinitionParser</span> <span class="n">parser</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="k">this</span><span class="o">.</span><span class="na">parsers</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">elementName</span><span class="o">,</span> <span class="n">parser</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>回到BeanDefinitionParserDelegate.parseCustomElement方法，最后一行handler.parse(ele, new ParserContext(this.readerContext, this, containingBd))，调用了对应的处理类的parse方法（<code>前面有提到的一个parsers成员变量，标签属性名-处理类实例映射</code>）</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">NamespaceHandlerSupport</span> <span class="kd">implements</span> <span class="n">NamespaceHandler</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 第一步
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kd">public</span> <span class="n">BeanDefinition</span> <span class="nf">parse</span><span class="o">(</span><span class="n">Element</span> <span class="n">element</span><span class="o">,</span> <span class="n">ParserContext</span> <span class="n">parserContext</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// 从parsers缓存中获取对应的BeanDefinitionParser处理类，调用parse方法
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">BeanDefinitionParser</span> <span class="n">parser</span> <span class="o">=</span> <span class="n">findParserForElement</span><span class="o">(</span><span class="n">element</span><span class="o">,</span> <span class="n">parserContext</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="o">(</span><span class="n">parser</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">?</span> <span class="n">parser</span><span class="o">.</span><span class="na">parse</span><span class="o">(</span><span class="n">element</span><span class="o">,</span> <span class="n">parserContext</span><span class="o">)</span> <span class="o">:</span> <span class="kc">null</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 第二步
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="nd">@Nullable</span>
</span></span><span class="line"><span class="cl">	<span class="kd">private</span> <span class="n">BeanDefinitionParser</span> <span class="nf">findParserForElement</span><span class="o">(</span><span class="n">Element</span> <span class="n">element</span><span class="o">,</span> <span class="n">ParserContext</span> <span class="n">parserContext</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="c1">// parsers这个变量很重要，里面预先缓存了标签属性名称-处理类实例的映射关系，这个方法作用
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="c1">// 就是根据标签属性名获取从parsers中获取处理类
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="n">String</span> <span class="n">localName</span> <span class="o">=</span> <span class="n">parserContext</span><span class="o">.</span><span class="na">getDelegate</span><span class="o">().</span><span class="na">getLocalName</span><span class="o">(</span><span class="n">element</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="n">BeanDefinitionParser</span> <span class="n">parser</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">parsers</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">localName</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="k">if</span> <span class="o">(</span><span class="n">parser</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">			<span class="n">parserContext</span><span class="o">.</span><span class="na">getReaderContext</span><span class="o">().</span><span class="na">fatal</span><span class="o">(</span>
</span></span><span class="line"><span class="cl">					<span class="s">&#34;Cannot locate BeanDefinitionParser for element [&#34;</span> <span class="o">+</span> <span class="n">localName</span> <span class="o">+</span> <span class="s">&#34;]&#34;</span><span class="o">,</span> <span class="n">element</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">		<span class="o">}</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">parser</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="11-总结">11 总结</h2>
<p>Spring解析XML，绕了这么大一圈。其目的就是把XML中配置的各种标签按照标签本身定义的解析方式进行处理，将处理的结果封装成BeanDefinition对象，并注入到<code>BeanFactory的成员变量BeanDefinitionRegistry</code>中。</p>
<p>这是Spring容器以XML方式初始化的第一步。其实无论是XML解析方式，还是注解方式，基本思想都是一样的。Spring容器在创建任何实例前，都会先搜集这个将要创建实例的一些必要信息，然后对这些信息根据一些扩展接口中提供的方法（模板设计模式），进行处理。</p>
<p>这些处理有一些是Spring内置的，可能也有使用者自定义的。具体的处理方式，在后面的章节体现。</p></div>

        <div id="donateDiv"><a id="donate">打赏</a></div>
<div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2020-11-22</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="/tags/spring%E6%BA%90%E7%A0%81/">spring源码</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/posts/life/memory-lol/" class="prev" rel="prev" title="Memory LOL"><i class="fas fa-angle-left fa-fw"></i>Memory LOL</a>
            <a href="/posts/spring/02-spring-tag-parse-bean-definition-parser/" class="next" rel="next" title="Spring源码 - Spring标签解析-BeanDefinitionParser">Spring源码 - Spring标签解析-BeanDefinitionParser<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
<div id="comments"><div id="valine" class="comment"></div><noscript>
                Please enable JavaScript to view the comments powered by <a href="https://valine.js.org/">Valine</a>.
            </noscript></div></article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.102.3">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2020 - 2024</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="https://wlizhi.cc" target="_blank">Wlizhi</a></span><span class="icp-splitter">&nbsp;|&nbsp;</span><br class="icp-br"/>
                    <span class="icp"><a href='https://beian.miit.gov.cn/' target='_blank'>豫ICP备17048163号<a/></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="/lib/valine/valine.min.css"><link rel="stylesheet" href="/lib/lightgallery/lightgallery.min.css"><script type="text/javascript" src="/lib/valine/Valine.min.js"></script><script type="text/javascript" src="/lib/smooth-scroll/smooth-scroll.min.js"></script><script type="text/javascript" src="/lib/autocomplete/autocomplete.min.js"></script><script type="text/javascript" src="/lib/algoliasearch/algoliasearch-lite.umd.min.js"></script><script type="text/javascript" src="/lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="/lib/lightgallery/lightgallery.min.js"></script><script type="text/javascript" src="/lib/lightgallery/lg-thumbnail.min.js"></script><script type="text/javascript" src="/lib/lightgallery/lg-zoom.min.js"></script><script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":35},"comment":{"valine":{"appId":"4yIHB0Rs1r3ykDVxayI5nqj3-gzGzoHsz","appKey":"ALr4H1hlK94girLDEUCGmp3o","avatar":"mp","el":"#valine","emojiCDN":"https://cdn.jsdelivr.net/npm/emoji-datasource-google@5.0.1/img/google/64/","emojiMaps":{"100":"1f4af.png","alien":"1f47d.png","anger":"1f4a2.png","angry":"1f620.png","anguished":"1f627.png","astonished":"1f632.png","black_heart":"1f5a4.png","blue_heart":"1f499.png","blush":"1f60a.png","bomb":"1f4a3.png","boom":"1f4a5.png","broken_heart":"1f494.png","brown_heart":"1f90e.png","clown_face":"1f921.png","cold_face":"1f976.png","cold_sweat":"1f630.png","confounded":"1f616.png","confused":"1f615.png","cry":"1f622.png","crying_cat_face":"1f63f.png","cupid":"1f498.png","dash":"1f4a8.png","disappointed":"1f61e.png","disappointed_relieved":"1f625.png","dizzy":"1f4ab.png","dizzy_face":"1f635.png","drooling_face":"1f924.png","exploding_head":"1f92f.png","expressionless":"1f611.png","face_vomiting":"1f92e.png","face_with_cowboy_hat":"1f920.png","face_with_hand_over_mouth":"1f92d.png","face_with_head_bandage":"1f915.png","face_with_monocle":"1f9d0.png","face_with_raised_eyebrow":"1f928.png","face_with_rolling_eyes":"1f644.png","face_with_symbols_on_mouth":"1f92c.png","face_with_thermometer":"1f912.png","fearful":"1f628.png","flushed":"1f633.png","frowning":"1f626.png","ghost":"1f47b.png","gift_heart":"1f49d.png","green_heart":"1f49a.png","grimacing":"1f62c.png","grin":"1f601.png","grinning":"1f600.png","hankey":"1f4a9.png","hear_no_evil":"1f649.png","heart":"2764-fe0f.png","heart_decoration":"1f49f.png","heart_eyes":"1f60d.png","heart_eyes_cat":"1f63b.png","heartbeat":"1f493.png","heartpulse":"1f497.png","heavy_heart_exclamation_mark_ornament":"2763-fe0f.png","hole":"1f573-fe0f.png","hot_face":"1f975.png","hugging_face":"1f917.png","hushed":"1f62f.png","imp":"1f47f.png","innocent":"1f607.png","japanese_goblin":"1f47a.png","japanese_ogre":"1f479.png","joy":"1f602.png","joy_cat":"1f639.png","kiss":"1f48b.png","kissing":"1f617.png","kissing_cat":"1f63d.png","kissing_closed_eyes":"1f61a.png","kissing_heart":"1f618.png","kissing_smiling_eyes":"1f619.png","laughing":"1f606.png","left_speech_bubble":"1f5e8-fe0f.png","love_letter":"1f48c.png","lying_face":"1f925.png","mask":"1f637.png","money_mouth_face":"1f911.png","nauseated_face":"1f922.png","nerd_face":"1f913.png","neutral_face":"1f610.png","no_mouth":"1f636.png","open_mouth":"1f62e.png","orange_heart":"1f9e1.png","partying_face":"1f973.png","pensive":"1f614.png","persevere":"1f623.png","pleading_face":"1f97a.png","pouting_cat":"1f63e.png","purple_heart":"1f49c.png","rage":"1f621.png","relaxed":"263a-fe0f.png","relieved":"1f60c.png","revolving_hearts":"1f49e.png","right_anger_bubble":"1f5ef-fe0f.png","robot_face":"1f916.png","rolling_on_the_floor_laughing":"1f923.png","scream":"1f631.png","scream_cat":"1f640.png","see_no_evil":"1f648.png","shushing_face":"1f92b.png","skull":"1f480.png","skull_and_crossbones":"2620-fe0f.png","sleeping":"1f634.png","sleepy":"1f62a.png","slightly_frowning_face":"1f641.png","slightly_smiling_face":"1f642.png","smile":"1f604.png","smile_cat":"1f638.png","smiley":"1f603.png","smiley_cat":"1f63a.png","smiling_face_with_3_hearts":"1f970.png","smiling_imp":"1f608.png","smirk":"1f60f.png","smirk_cat":"1f63c.png","sneezing_face":"1f927.png","sob":"1f62d.png","space_invader":"1f47e.png","sparkling_heart":"1f496.png","speak_no_evil":"1f64a.png","speech_balloon":"1f4ac.png","star-struck":"1f929.png","stuck_out_tongue":"1f61b.png","stuck_out_tongue_closed_eyes":"1f61d.png","stuck_out_tongue_winking_eye":"1f61c.png","sunglasses":"1f60e.png","sweat":"1f613.png","sweat_drops":"1f4a6.png","sweat_smile":"1f605.png","thinking_face":"1f914.png","thought_balloon":"1f4ad.png","tired_face":"1f62b.png","triumph":"1f624.png","two_hearts":"1f495.png","unamused":"1f612.png","upside_down_face":"1f643.png","weary":"1f629.png","white_frowning_face":"2639-fe0f.png","white_heart":"1f90d.png","wink":"1f609.png","woozy_face":"1f974.png","worried":"1f61f.png","yawning_face":"1f971.png","yellow_heart":"1f49b.png","yum":"1f60b.png","zany_face":"1f92a.png","zipper_mouth_face":"1f910.png","zzz":"1f4a4.png"},"enableQQ":true,"highlight":true,"lang":"zh-cn","meta":["nick","mail","link"],"pageSize":5,"placeholder":"昵称栏输入QQ，会使用QQ昵称和头像、并自动补充QQ邮箱到邮箱栏。\r\n正确填写邮箱更容易及时得到回复。\r\n说点什么吧...","recordIP":true,"visitor":true}},"lightGallery":{"actualSize":false,"exThumbImage":"data-thumbnail","hideBarsDelay":2000,"selector":".lightgallery","speed":400,"thumbContHeight":80,"thumbWidth":80,"thumbnail":true},"search":{"algoliaAppID":"1U459KF21F","algoliaIndex":"LoveItSite","algoliaSearchKey":"0433ffe95d71201a4f7c3e04b0125ac5","highlightTag":"em","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"algolia"}};</script><script type="text/javascript" src="/js/theme.min.js"></script><script type="text/javascript" src="/js/donate.js"></script></body>
</html>
